package com.limingfeiyu.platform.management.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.limingfeiyu.platform.management.entity.HospitalPackageItem;
import com.limingfeiyu.platform.management.mapper.HospitalPackageItemMapper;
import com.limingfeiyu.platform.management.service.IHospitalPackageItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 医院_套餐_项目 服务实现类
 * </p>
 *
 * @author 黎铭飞羽
 * @since 2024-04-20
 */
@Service
@DS("psb")
public class HospitalPackageItemServiceImpl extends ServiceImpl<HospitalPackageItemMapper, HospitalPackageItem>
        implements IHospitalPackageItemService {

    @Override
    public boolean emptyPackageIdItems(List<HospitalPackageItem> obj) {
        boolean bool = true;
        // 套餐ID
        Long packageId = 0L;
        for (HospitalPackageItem hospitalPackageItem : obj) {
            // 如果套餐ID和上一个套餐ID相同，跳过这次的删除操作
            if (packageId.equals(hospitalPackageItem.getPackageId())) {
                continue;
            }
            try {
                // 如果删除失败，停止本次操作
                LambdaQueryWrapper<HospitalPackageItem> packageItemLqw = new LambdaQueryWrapper<>();
                packageItemLqw.eq(HospitalPackageItem::getPackageId, hospitalPackageItem.getPackageId());
                // 保留删除套餐ID
                packageId = hospitalPackageItem.getPackageId();
                super.remove(packageItemLqw);
            } catch (Exception e) {
                e.printStackTrace();
                bool = false;
                break;
            }
        }
        return bool;
    }

    @Override
    public List<HospitalPackageItem> getPackageItemListByPackageId(Long packageId) {
        LambdaQueryWrapper<HospitalPackageItem> hospitalPackageItemLqw = new LambdaQueryWrapper<>();
        hospitalPackageItemLqw.eq(HospitalPackageItem::getPackageId, packageId);
        return list(hospitalPackageItemLqw);
    }
}
